package 字节;

import java.util.LinkedList;
import java.util.List;

/**
 * @author zhangmin
 * @create 2022-04-12 15:50
 */
public class 面试0412 {
    public static class Node{
        private int data;
        public List<Node> fields;
        public Node(int n){
            this.data=n;
        }
        public Node(int n,List<Node> f){
            this.data=n;
            this.fields=f;
        }
        //实现拷贝root为根的这颗树，要求每个节点都和原来的节点执行不同的对象
        public Node dup(Node root){
            if (root==null) return null;
            List<Node> rootfields = root.fields;
            List<Node> field=new LinkedList<>();
            for (int i = 0; i < rootfields.size(); i++) {
                //递归构造子节点
                field.add(dup(rootfields.get(i)));
            }
            Node newroot=new Node(root.data,field);
            return newroot;
        }
    }
}
